<template>
  <div>
    <h1 align="center">发布新课程</h1>
    <el-steps :active="1" finish-status="success">
      <el-step title="填写课程基本信息"></el-step>
      <el-step title="创建课程大纲"></el-step>
      <el-step title="最终发布"></el-step>
    </el-steps>
    <br />
    <el-row>
      <el-col :span="20">
        <el-form
          status-icon
          ref="info"
          label-width="200px"
          class="demo-ruleForm"
        >
          <el-form-item label="课程标题" required>
            <el-input v-model.trim="courseInfo.title"></el-input>
          </el-form-item>
          <el-form-item label="总课时"
            ><el-input-number
              v-model="courseInfo.lessonNum"
              :min="0"
              label="描述文字"
            ></el-input-number
          ></el-form-item>
          <el-form-item label="讲师"
            ><el-select
              v-model="courseInfo.teacherId"
              clearable
              placeholder="选择讲师"
            >
              <el-option
                v-for="teacher in teacherList"
                :key="teacher.id"
                :label="teacher.name"
                :value="teacher.id"
              >
              </el-option> </el-select
          ></el-form-item>
          <el-form-item label="专业">
            <el-select
              v-model="courseInfo.subjectParentId"
              placeholder="请选择专业"
              @change="cut_two_title"
            >
              <el-option
                v-for="one_title in one_title_classify"
                :key="one_title.id"
                :label="one_title.title"
                :value="one_title.id"
              >
              </el-option>
            </el-select>

            <el-select
              v-model="courseInfo.subjectId"
              collapse-tags
              style="margin-left: 20px"
              placeholder="请选择专业2"
            >
              <el-option
                v-for="two_title in two_title_classify"
                :key="two_title.id"
                :label="two_title.title"
                :value="two_title.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="课程封面">
            <el-upload
              ref="upload"
              class="avatar-uploader"
              :action="BASE_API + '/ossService/fileOss/upload'"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img
                v-if="courseInfo.cover"
                :src="courseInfo.cover"
                class="avatar"
              />

              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="课程简介">
            <el-input
              v-model="courseInfo.description"
              type="textarea"
              :rows="10"
            ></el-input>
          </el-form-item>
          <el-form-item label="课程价格">
            <el-input-number
              v-model="courseInfo.price"
              controls-position="right"
              :min="0"
            ></el-input-number>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="save_basic_info"
              >保存并下一步</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import course from "@/api/edu/course";
import teacher from "@/api/edu/teacher";
import subject from "@/api/edu/subject";
export default {
  data() {
    return {
      BASE_API: process.env.VUE_APP_BASE_API,
      //讲师姓名
      teacherList: [
        {
          id: null,
          name: null,
        },
      ],
      //一级专业分类名称
      one_title_classify: [
        {
          id: null,
          title: null,
          children: [
            {
              id: null,
              title: null,
            },
          ],
        },
      ],
      //二级专业分类名称
      two_title_classify: [
        {
          id: null,
          title: null,
        },
      ],
      //courseInfo信息收集
      courseInfo: {
        id:null,
        cover: null,
        description: null,
        lessonNum: null,
        price: null,
        subjectId: null,
        subjectParentId: null,
        teacherId: null,
        title: null,
      },
    };
  },

  methods: {
    //添加课程基本信息
    save_basic_info() {
      if (this.courseInfo.id==null) {
        course.save_basic_info(this.courseInfo).then((Response) => {
          // Response.data.id
          this.$router.push("/course/chapter/" + Response.data.id);
          this.$message.success("添加课程基本信息成功");
        });
      }else{
        this.update_basic_info();
      }
    },
    //修改课程基本信息
    update_basic_info() {
      course.update_basic_info(this.courseInfo).then((Response) => {
        // Response.data.id
        this.$router.push("/course/chapter/" + Response.data.id);
        this.$message.success("修改课程基本信息成功");
      });
    },
    //获取讲师姓名 id
    get_teacher_name_list() {
      teacher.get_all_teacher().then((Response) => {
        this.teacherList = Response.data.teacherList;
      });
    },
    //获取专业分类
    get_subject_title() {
      subject.get_title_list().then((Response) => {
        this.one_title_classify = Response.data.TitleClassify;
      });
    },
    //二级标题的切换
    cut_two_title(value) {
      this.courseInfo.subjectId = null;
      for (var i = 0; i < this.one_title_classify.length; i++) {
        var one_title = this.one_title_classify[i];
        if (one_title.id === value) {
          this.two_title_classify = one_title.children;
        }
      }
    },
    //文件成功上传的钩子
    handleAvatarSuccess(res, file) {
      this.courseInfo.cover = res.data.url;
    },
    //上传文件之前的钩子
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    //数据返显
    fetch_basic_info(id){
      course.get_basic_info(id).then((Response) => {
        this.courseInfo = Response.data.courseInfo;
        //专业
        subject.get_title_list().then(res=>{
          var one_titles = res.data.TitleClassify;
          for(var i=0;i<one_titles.length;i++){
            var one_title=one_titles[i];
            if(this.courseInfo.subjectParentId==one_title.id){
               this.two_title_classify = one_title.children
            }
          }
        })
        
      })
    }
  },
  mounted() {
    this.get_teacher_name_list();
    this.get_subject_title();
    if (this.$route.params && this.$route.params.id) {
      const id = this.$route.params.id;
      this.fetch_basic_info(id);
      }
  }
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>